/*
 *  Class name: NavigationMenuBean.java
 *  Version: 1.0
 *  Date: 20.11.2013
 */
package cz.muni.fi.pa165.soccergamesarchive.managedbeans.application;

import java.io.Serializable;
import java.util.Locale;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import static cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper.ADD_SECTION;
import static cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper.EDITOR;
import static cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper.EDIT_SECTION;
import static cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper.MATCH_MODULE;
import static cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper.NAVIGATION_PARAMS;
import static cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper.PLAYER_MODULE;
import static cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper.PRIVATE_AREA;
import static cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper.TEAM_MODULE;
import static cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper.VIEW_SECTION;
import static javax.faces.context.FacesContext.getCurrentInstance;

/**
 *
 * @author Michal Ďuriš
 */
@Named(value = "navigationMenuBean")
@SessionScoped
public class NavigationMenuBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String DELIMETER = "_";

    private static final String DEFAULT_HOME_PATH = "/index" + NAVIGATION_PARAMS;

    private static final String TEAM_HOME_PATH = "/" + TEAM_MODULE + "/"
                                                 + VIEW_SECTION + NAVIGATION_PARAMS;
    private static final String PLAYER_HOME_PATH = "/" + PLAYER_MODULE + "/"
                                                   + VIEW_SECTION + NAVIGATION_PARAMS;
    private static final String MATCH_HOME_PATH = "/" + MATCH_MODULE + "/"
                                                  + VIEW_SECTION + NAVIGATION_PARAMS;

    private static final String TEAM_VIEW_MENU_ITEM = TEAM_MODULE + DELIMETER
                                                      + VIEW_SECTION;
    private static final String TEAM_ADD_MENU_ITEM = TEAM_MODULE + DELIMETER
                                                     + ADD_SECTION;
    private static final String TEAM_EDIT_MENU_ITEM = TEAM_MODULE + DELIMETER
                                                      + EDIT_SECTION;

    private static final String PLAYER_VIEW_MENU_ITEM = PLAYER_MODULE + DELIMETER
                                                        + VIEW_SECTION;
    private static final String PLAYER_ADD_MENU_ITEM = PLAYER_MODULE + DELIMETER
                                                       + ADD_SECTION;
    private static final String PLAYER_EDIT_MENU_ITEM = PLAYER_MODULE + DELIMETER
                                                        + EDIT_SECTION;

    private static final String MATCH_VIEW_MENU_ITEM = MATCH_MODULE + DELIMETER
                                                       + VIEW_SECTION;
    private static final String MATCH_ADD_MENU_ITEM = MATCH_MODULE + DELIMETER
                                                      + ADD_SECTION;
    private static final String MATCH_EDIT_MENU_ITEM = MATCH_MODULE + DELIMETER
                                                       + EDIT_SECTION;

    private String selectedMenuItem;

    /**
     * Creates a new instance of NavigationMenuBean
     */
    public NavigationMenuBean() {
    }

    public String getSelectedMenuItem() {
        return selectedMenuItem;
    }

    public void setSelectedMenuItem(String selectedMenuItem) {
        this.selectedMenuItem = selectedMenuItem;
    }

    public void saveSelectedItem(ActionEvent event) {
        selectedMenuItem = (String) event.getComponent().getAttributes().get(
                "name");
    }

    public String getTEAM_VIEW_MENU_ITEM() {
        return TEAM_VIEW_MENU_ITEM;
    }

    public String getTEAM_ADD_MENU_ITEM() {
        return TEAM_ADD_MENU_ITEM;
    }

    public String getTEAM_EDIT_MENU_ITEM() {
        return TEAM_EDIT_MENU_ITEM;
    }

    public String getPLAYER_VIEW_MENU_ITEM() {
        return PLAYER_VIEW_MENU_ITEM;
    }

    public String getPLAYER_ADD_MENU_ITEM() {
        return PLAYER_ADD_MENU_ITEM;
    }

    public String getPLAYER_EDIT_MENU_ITEM() {
        return PLAYER_EDIT_MENU_ITEM;
    }

    public String getMATCH_VIEW_MENU_ITEM() {
        return MATCH_VIEW_MENU_ITEM;
    }

    public String getMATCH_ADD_MENU_ITEM() {
        return MATCH_ADD_MENU_ITEM;
    }

    public String getMATCH_EDIT_MENU_ITEM() {
        return MATCH_EDIT_MENU_ITEM;
    }

    public String getHomeLocation() {

        HttpServletRequest servletRequest
                = (HttpServletRequest) getCurrentInstance().getExternalContext()
                .getRequest();

        String homeDestination;
        String location = "default";
        String servletPath = servletRequest.getServletPath();

        if (servletPath != null && !servletPath.isEmpty()) {

            String[] paths = servletPath.split("/");

            if (paths.length >= 2) {

                location = paths[1];
            }
        }

        switch (location.toLowerCase(Locale.ENGLISH)) {
            case TEAM_MODULE:
                homeDestination = TEAM_HOME_PATH;
                selectedMenuItem = TEAM_VIEW_MENU_ITEM;
                break;
            case PLAYER_MODULE:
                homeDestination = PLAYER_HOME_PATH;
                selectedMenuItem = PLAYER_VIEW_MENU_ITEM;
                break;
            case MATCH_MODULE:
                homeDestination = MATCH_HOME_PATH;
                selectedMenuItem = MATCH_VIEW_MENU_ITEM;
                break;
            default:
                homeDestination = DEFAULT_HOME_PATH;
                selectedMenuItem = null;
        }

        return homeDestination;
    }

    public String goToLocation(String module, String section) {

        if (module == null || module.isEmpty()
            || section == null || section.isEmpty()) {

            return "";
        }

        switch (section) {
            case ADD_SECTION:
            case EDIT_SECTION:
                 selectedMenuItem = module + DELIMETER + section;
                return "/" + PRIVATE_AREA + "/" + module + "/" + EDITOR + NAVIGATION_PARAMS;
            case VIEW_SECTION:
                 selectedMenuItem = module + DELIMETER + section;
                return "/" + module + "/" + section + NAVIGATION_PARAMS;
            default:
                //TODO: LOGGER
                System.err.println("Unknown section !!!"
                        + "Method: goToLocation"
                        + "Class: " + getClass().getName());
        }

        return "";
    }

}
